# -*-coding:utf-8-*-
from collections import OrderedDict

from django.core.cache import cache

from apps.goods.models import GoodsChannel


# 获取商品分类三级表数据
def get_categories():
    """1 提供导航菜单数据"""
    # json_dict = {
    #     "1": {
    #         "channels": [
    #             {"id": 1, "name": "手机", "url": "http://shouji.jd.com/"},
    #             {"id": 2, "name": "相机", "url": "http://www.itcast.cn/"}
    #         ],
    #         "sub_cats": [
    #             {
    #                 "id": 38,
    #                 "name": "手机通讯",
    #                 "sub_cats": [
    #                     {"id": 115, "name": "手机"},
    #                     {"id": 116, "name": "游戏手机"}
    #                 ]
    #             },
    #             {
    #                 "id": 39,
    #                 "name": "手机配件",
    #                 "sub_cats": [
    #                     {"id": 119, "name": "手机壳"},
    #                     {"id": 120, "name": "贴膜"}
    #                 ]
    #             }
    #         ]
    #     },
    #     "2": {
    #         "channels": [
    #             {"id": 1, "name": "电脑", "url": "http://shouji.jd.com/"},
    #             {"id": 2, "name": "办公", "url": "http://www.itcast.cn/"}
    #         ],
    #         "sub_cats": [
    #             {
    #                 "id": 38,
    #                 "name": "电脑配件",
    #                 "sub_cats": [
    #                     {"id": 115, "name": "键盘"},
    #                     {"id": 116, "name": "鼠标"}
    #                 ]
    #             },
    #             {
    #                 "id": 39,
    #                 "name": "办公文具",
    #                 "sub_cats": [
    #                     {"id": 119, "name": "橡皮"},
    #                     {"id": 120, "name": "桌子"}
    #                 ]
    #             }
    #         ]
    #     },
    #     "3": {
    #         "channels": [
    #             {"id": 1, "name": "男装", "url": "http://shouji.jd.com/"},
    #             {"id": 2, "name": "女装", "url": "http://www.itcast.cn/"}
    #         ],
    #         "sub_cats": [
    #             {
    #                 "id": 38,
    #                 "name": "男士上衣",
    #                 "sub_cats": [
    #                     {"id": 115, "name": "西服"},
    #                     {"id": 116, "name": "裤子"}
    #                 ]
    #             },
    #             {
    #                 "id": 39,
    #                 "name": "男士短袖",
    #                 "sub_cats": [
    #                     {"id": 119, "name": "休闲"},
    #                     {"id": 120, "name": "运动"}
    #                 ]
    #             }
    #         ]
    #     }
    # }

    # 取所有37个商品分类,按照组组序号,权重排序
    categories = cache.get('goods_categories')
    if categories:
        return categories

    channels = GoodsChannel.objects.order_by('group_id', 'sequence')
    categories = OrderedDict()

    # 遍历37个一级分类
    for channel in channels:
        group_id = channel.group_id
        # 构造返回数据结构, 排除重复id
        if group_id not in categories:
            categories[group_id] = {"channels": [], "sub_cats": []}
        # 一级列表
        cat1 = channel.category
        cat1.url = channel.url
        categories[channel.group_id]["channels"].append(cat1)
        # 二级列表
        for cat2 in cat1.subs.all():
            cat2.sub_cats = [cat3 for cat3 in cat2.subs.all()]  # 将cat3添加为cat2的'sub_cats'属性
            categories[channel.group_id]["sub_cats"].append(cat2)

    cache.set(key='goods_categories', value=categories, timeout=20*60)
    return categories


# 面包屑导航
def get_breadcrumb(cat3):
    """
    获取面包屑导航
    :param category: 商品类别三级
    :return: 面包屑导航字典
    """
    breadcrumb = {
        "cat3": cat3,
        "cat2": cat3.parent,
        "cat1": cat3.parent.parent,
    }

    return breadcrumb
